package org.jeecg.modules.yw.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.yw.entity.ContractAttachment;
import org.jeecg.modules.yw.entity.ContractInfo;
import org.jeecg.modules.yw.mapper.ContractInfoMapper;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.vo.ContractInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ContractInfoService extends ServiceImpl<ContractInfoMapper, ContractInfo> {
    @Autowired
    private ContractAttachmentService contractAttachmentService;
    public IPage<ContractInfoVo> pageInfo(Page<ContractInfo> page, String name, String mobile, String[] areas) {
        IPage<ContractInfo> contractInfoIPage = baseMapper.selectPageVo(page, name,mobile,areas);
        List<ContractInfo> records = contractInfoIPage.getRecords();
        IPage<ContractInfoVo> result = new Page<>();
        List<ContractInfoVo> recordList = new ArrayList<>();
        if (records != null && records.size() > 0) {
            for (ContractInfo contractInfo : records) {
                ContractInfoVo contractInfoVo = new ContractInfoVo();
                BeanUtils.copyProperties(contractInfo, contractInfoVo);
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("contract_id", contractInfo.getId());
                queryWrapper.orderByDesc("sort");
                List<ContractAttachment> list=contractAttachmentService.list(queryWrapper);
                if (list != null && list.size() > 0) {
                    List<String> pics=new ArrayList<>();
                    for (ContractAttachment contractAttachment : list) {
                        pics.add(contractAttachment.getAttachmentPath());
                    }
                    contractInfoVo.setPics(pics);
                }
                recordList.add(contractInfoVo);
            }
        }
        result.setPages(contractInfoIPage.getPages());
        result.setTotal(contractInfoIPage.getTotal());
        result.setCurrent(contractInfoIPage.getCurrent());
        result.setSize(contractInfoIPage.getSize());
        result.setRecords(recordList);
        return result;
    }

    public void saveBatchContractInfoPics(List<String> pics, String id) {
        int i = 1;
        for (String s : pics) {
            ContractAttachment contractAttachment = new ContractAttachment();
            contractAttachment.setId(CommonUtils.getID());
            contractAttachment.setContractId(id);
            contractAttachment.setAttachmentPath(s);
            contractAttachment.setSort(i);
            contractAttachmentService.save(contractAttachment);
            i++;
        }
    }

    public boolean deleteContractInfoPics(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("contract_id", id);
        if (contractAttachmentService.remove(queryWrapper)) {
            return true;
        }
        return false;
    }
}
